# conding=utf-8


from matplotlib.colors import cnames


# By: Jamiexu
# Dtae: 2022-04-21
# Blog: https://blog.jamiexu.cn

import threading
import AudioUtils
import Image
import Reply
import AudioConfig

AUDIO_OUTPUT = AudioConfig.BASE_PATH + '\\w.wav'

AUDIO_FORMAT = 'wav'


def start():
    StartThread().start()


class StartThread (threading.Thread):
	def run(self):
		print('欢迎使用人工智障语音聊天')
		AudioUtils.say('欢迎使用人工智障语音聊天')
		# time.sleep(1)
		while(AudioConfig.get_state()):

			if AudioUtils.recordAudio() == -1:
				continue

			print("\n\nIdentify On Network...")
			result = AudioUtils.recognizeSound()  # 识别语音指令

			# print(result)
			if isinstance(result, dict):
				continue

			print("==================================================")
			print("Identify Result: %s" % result)

			if AudioConfig.get_image():
				AudioUtils.say('开始采集中，请勿关闭窗口哟', 6)
				Image.get_image(result, 3)

			elif result.find("图片") != -1:
				AudioConfig.set_image(True)
				AudioUtils.say('请说出您想采集的图片', 6)
				Image.timer_start()

			else:
				result = Reply.reply(result[0])
				print('Reply you: {}'.format(result))
				AudioUtils.say(result)

				if result.find("退出") != -1:
					AudioUtils.say('主人我走咯，记得想我哟！')
					AudioConfig.set_state(False)
					break
			AudioConfig.set_state(True)
			# time.sleep(5)


if __name__ == "__main__":
    start()
